//
//  PhotoBrowserAnimator.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/20.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit
import SDWebImage

// MARK: - 展现动画代理协议
protocol PhotoBrowserPresentDelegate: NSObjectProtocol {
    /// 展现动画需要的图片
    ///
    /// - parameter indexPath: 图片索引
    ///
    /// - returns:  UIImageView
    func imageViewForPresentAnimator(indexPath: NSIndexPath) -> UIImageView
    
    /// 展现动画起始位置
    ///
    /// - parameter indexPath: 图片索引
    ///
    /// - returns:  CGrect
    func fromRectForPresentAnimator(indexPath: NSIndexPath) -> CGRect
    
    /// 展现动画结束位置
    ///
    /// - parameter IndexPath: 图片索引
    ///
    /// - returns:  CGRect
    func toRectForPresentAnimator(IndexPath: NSIndexPath) -> CGRect
}

// MARK: - 接触转场动画代理协议
protocol PhotoBrowserDismissDelegate: NSObjectProtocol {
    func imageViewForDismissAnimator() -> UIImageView
    
    func indexPathForDismissAnimator() -> NSIndexPath
}

 // MARK: - 提供动画转场相关的代理类
class PhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {
    
    /// 实现协议的代理
    private weak var presentDelegate: PhotoBrowserPresentDelegate?
    private weak var dismissDelegate: PhotoBrowserDismissDelegate?
    /// 图片索引
    private var indexpath: NSIndexPath?
    
    /// 是否展现 标记
    var ispresented = false
    
    /// 设置属性
    ///
    /// - parameter delegate:  动画代理
    /// - parameter indexpath: 图片索引
    func setDelegateAndParameters(presentDelegate: PhotoBrowserPresentDelegate, dismissDelegate: PhotoBrowserDismissDelegate, indexpath: NSIndexPath) {
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexpath = indexpath
    }
    
    /// 提供转场动画代理
    ///
    /// - returns: 呈现动画代理对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        ispresented = true
        return self
    }
    
    /// 解除动画代理
    ///
    /// - returns: 解除动画代理对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        ispresented = false
        return self
    }
}

// MARK: - 动画实现
extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {
    
    /// 动画转场 展现动画 / 解除动画
    ///
    /// - parameter transitionContext: 转场上下文
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

//        let toview = transitionContext.viewForKey(UITransitionContextToViewKey)!//browser 的根视图

        ispresented ? presentationAnimation(transitionContext) : dismissAnimation(transitionContext)
        
    }
    
    /// 展现动画
    ///
    /// - parameter transitionContext: transitionContext
    func presentationAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let delegate = presentDelegate, indexpath = indexpath else {
            return
        }
        
        //toview
        let toview = transitionContext.viewForKey(UITransitionContextToViewKey)
        toview?.alpha = 0
        transitionContext.containerView()?.addSubview(toview!)

        // 图片
        let imgView = delegate.imageViewForPresentAnimator(indexpath)
        imgView.frame = delegate.fromRectForPresentAnimator(indexpath)
        transitionContext.containerView()?.addSubview(imgView)
        
        //动画
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            imgView.frame = delegate.toRectForPresentAnimator(indexpath)
            
            }) { (Bool) -> Void in
                toview?.alpha = 1
                imgView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    /// 解除动画
    ///
    /// - parameter transitionContext: transitionContext
    func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate = presentDelegate, dismissDelegate = dismissDelegate else {
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let imgView = dismissDelegate.imageViewForDismissAnimator()
        transitionContext.containerView()?.addSubview(imgView)
        
                    fromView.alpha = 0
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            let indexpath = dismissDelegate.indexPathForDismissAnimator()
            imgView.frame = presentDelegate.fromRectForPresentAnimator(indexpath)
            }) { (Bool) -> Void in
                
                imgView.removeFromSuperview()
                fromView.removeFromSuperview()
                transitionContext.completeTransition(true)

        }
    }
    
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
       return 10
    }
    
    
    
}


